// pages/game/game.js
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    items: [
      {value: 0, name: '可回收垃圾'},
      {value: 1, name: '有害垃圾'},
      {value: 2, name: '厨余垃圾'},
      {value: 3, name: '其他垃圾'}
    ],
    info: [],
    trashType:0
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    var that = this
    wx.request({
      url: 'https://api.tianapi.com/txapi/anslajifenlei/index?key=436036e6177d1b5f93c17e7932962bf2',
      success: function (res) {
        console.log(res.data)
        that.setData({
          info: res.data,
          trashType: res.data.newslist[0].type
        })
      }
    })
  },

  correctAns:function(){
    wx.showToast({
      title: '正确！', // 标题
      icon: 'success',  // 图标类型，默认success
      duration: 1500  // 提示窗停留时间，默认1500ms
    })
    this.onLoad()
  },
  wrongAns_rec:function(){
    wx.showToast({
      title: '错误！应为可回收垃圾', // 标题
      icon: 'none',  // 图标类型，默认success
      duration: 1500  // 提示窗停留时间，默认1500ms
    })
    this.onLoad()
  },

  wrongAns_harm:function(){
    wx.showToast({
      title: '错误！应为有害垃圾', // 标题
      icon: 'none',  // 图标类型，默认success
      duration: 1500  // 提示窗停留时间，默认1500ms
    })
    this.onLoad()
  },

  wrongAns_kit:function(){
    wx.showToast({
      title: '错误！应为厨余垃圾', // 标题
      icon: 'none',  // 图标类型，默认success
      duration: 1500  // 提示窗停留时间，默认1500ms
    })
    this.onLoad()
  },

  wrongAns_oth:function(){
    wx.showToast({
      title: '错误！应为其它垃圾', // 标题
      icon: 'none',  // 图标类型，默认success
      duration: 1500  // 提示窗停留时间，默认1500ms
    })
    this.onLoad()
  },

  nextQuestion:function(){
    this.onLoad()
  },


  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  radioChange(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)

    const items = this.data.items
    for (let i = 0, len = items.length; i < len; ++i) {
      items[i].checked = items[i].value === e.detail.value
    }

    this.setData({
      items
    })
  },
  next:function(){
    this.onLoad()
  }
})
